<template>
	<view class="content">
		<!-- 如果没有文章 -->
		<view v-if="!articleList.length">
			<image style="margin:10%;" src='../../../static/null/null.png'></image>
		</view>
		<!-- 文章列表 -->
		<view class="article-item" v-for="(item, index) in articleList" :key="index" @click="goDetail(item)">
			<image class="cover" :src="item.cover"></image>
			<view class="info">
				<text class="title">{{ item.title }}</text>
				<view class="meta">
					<text class="author">#{{ item.author }}</text>
					<text class="creation-time">{{ formatDate(item.creationTime) }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getArticleList
	} from "@/api/extension.js";

	export default {
		data() {
			return {
				articleList: [],
				pageNumber: 1,
				pageSize: 20,
				type: ''
			};
		},
		onLoad(options) {
			console.log(options.type, "options");
			this.type = options.type;
			this.getList();
		},
		methods: {
			getList() {
				const data = {
					type: this.type,
					pageSize: this.pageSize,
					pageNumber: this.pageNumber
				};
				getArticleList(data).then(res => {
					console.log(res.data);
					this.articleList = [...this.articleList, ...res.data.records];
					if (res.data.records.length < this.pageSize) {
						// 没有更多数据时取消监听
						uni.offReachBottom();
					}
				});
			},
			// 格式化日期
			formatDate(dateString) {
				const date = new Date(dateString);
				return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
			},
			goDetail(item){
				uni.navigateTo({
					url: '/pages/index/article/index?id='+item.id
				});
			}
		},
		onReachBottom() {
			// 当页面滚动到底部时触发
			this.pageNumber++;
			this.getList();
		}
	};
</script>

<style lang="scss">
	@import '@/uni.scss';

	.content {
		min-height: 100vh;
		background-color: $uni-bg-color-grey;
	}

	.article-item {
		display: flex;
		padding: 20rpx;
		margin-bottom: 10rpx;
		background-color: white;
		margin: 20rpx;
		border-radius: 20rpx;

		& .cover {
			width: 200rpx;
			height: 150rpx;
			flex-shrink: 0;
			margin-right: 20rpx;
			border-radius: 8rpx;
		}

		& .info {
			flex-grow: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		}

		& .title {
			font-size: 30rpx;
			font-weight: bold;
			color: #333;
			line-height: 1.5;
		}

		& .meta {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 24rpx;
			color: #999;
		}
	}
</style>